package com.thf.controller.echarts;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.thf.model.EchartsOneModel;
import com.thf.model.ResultData;
import com.thf.service.EchartsOneService;
import com.thf.util.ResultTool;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@RestController
public class EchartsOneExportExcelController {
    @Resource
    private EchartsOneService echartsOneService;

    @RequestMapping("/back/echartOne/excel")
    public String exportExcel(){
        List<EchartsOneModel> dataList = echartsOneService.find();
        if(dataList==null || dataList.size() <= 0){
            return JSON.toJSONString(new ResultData<>(ResultTool.CODE_ERROR));
        }
        String filePath = "d:/excelData/"+System.currentTimeMillis();
        Random random=new Random();
        for (int i = 0 ;i<4;i++){
            filePath+=random.nextInt(10);
        }
        filePath+=".xls";
        EasyExcel.write(filePath, EchartsOneModel.class).sheet("折线图").doWrite(dataList);
        return JSON.toJSONString(new ResultData<>(ResultTool.CODE_SUCCES));
    }
}
